/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package korekosystem.vista;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import korekosystem.KorekoSystem;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;

/**
 *
 * @author Wise_2
 */
public class compraxmes_prueba extends javax.swing.JFrame {

    String mes, anno;
    String PATH;
    //Calendar cldr;
    Date dato;
    SimpleDateFormat sdf;
    HashMap parametrosMap;
    static Connection conexion;
    /**
     * Creates new form compraxmes_prueba
     */
    public compraxmes_prueba() {
        setTheFeel();
        initComponents();
        PATH = "/korekosystem/reportes/";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox_mes = new javax.swing.JComboBox();
        jButton_crear = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField_anno = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Informe Compra por Mes");
        setBackground(null);

        jComboBox_mes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Setiembre", "Octubre", "Noviembre", "Diciembre" }));

        jButton_crear.setText("Crear");
        jButton_crear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_crearActionPerformed(evt);
            }
        });

        jLabel1.setText("Anno");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_crear)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jComboBox_mes, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(73, 73, 73)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(jTextField_anno, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField_anno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62)
                .addComponent(jButton_crear)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jButton_crear.getAccessibleContext().setAccessibleName("button_Crear");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setTheFeel(){
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(compraxmes_prueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(compraxmes_prueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(compraxmes_prueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(compraxmes_prueba.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }
    
    private void jButton_crearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_crearActionPerformed
        // TODO add your handling code here:
        mes = jComboBox_mes.getSelectedItem().toString();
        anno = jTextField_anno.getText();
        sdf = new SimpleDateFormat("MMMMM-yyyy");
        //cldr = Calendar.getInstance();
        parametrosMap = new HashMap();
        try{
            dato = sdf.parse(mes + "-" + anno);
            //cldr.setTime(dato);
            parametrosMap.put("annoMes", dato);
            System.out.println(dato);
            //System.out.println(cldr.get(Calendar.MONTH) + " " +cldr.get(Calendar.YEAR));
            crearConexion();
            crearInforme();
        }
        catch(java.text.ParseException e){
            e.printStackTrace();
        }
        finally{
            cerrarConexion();
        }
        
    }//GEN-LAST:event_jButton_crearActionPerformed

    private void crearConexion(){
        String usuario, password, host, puerto, database, driver, urljdbc;
        
        usuario = "groupwe_anamaria";
        password = "anamaria";
        host = "212.1.210.181";
        puerto = "3306";
        database = "groupwe_anamaria";
        driver = "com.mysql.jdbc.Driver";
        urljdbc = "jdbc:mysql://" + host + ":" + puerto + "/" + database;
        
        conexion = null;
        
        try{
            Class.forName(driver).newInstance();
            conexion = DriverManager.getConnection(urljdbc,usuario,password);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
    }
    private void cerrarConexion(){
        try{
            if(conexion != null){
                conexion.close();
            }
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    private void crearInforme(){
        try{
            //Path to your .jasper file in your package
            //reporteF = new File("C:/Users/Wise_2/Documents/NetBeansProjects/%20etyc-and-koreko/koreko/src/KorekoSystem/src/korekosystem/reportes/reporte-compraxmes_0.jasper");
            
            JasperReport reporte = (JasperReport)JRLoader.loadObject(KorekoSystem.class.getResourceAsStream(PATH + "reporte-compraxmes_2.jasper"));
            
            //Fill the report with parameter, connection and the stream reader (file)
            JasperPrint reportePrint = JasperFillManager.fillReport(reporte, parametrosMap, conexion);
            
            //Viewer for JasperReport
            JRViewer reportePrintV = new JRViewer(reportePrint);
            
            //Insert viewer to a JFrame to make it showable
            JFrame jf = new JFrame();
            jf.getContentPane().add(reportePrintV);
            jf.validate();
            jf.setVisible(true);
            jf.setSize(new Dimension(800,600));
	    jf.setLocation(300,100);
	    jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
        }
        catch(JRException e){
            e.printStackTrace();
        }
        
    }
    
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_crear;
    private javax.swing.JComboBox jComboBox_mes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField_anno;
    // End of variables declaration//GEN-END:variables
}
